package com.hhd.pdf.components.row;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.hhd.pdf.components.AbstractPdfComponent;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.hhd.pdf.variable.VariableContext;
import com.hhd.pdf.CommonUtils;
import org.dom4j.Attribute;
import org.dom4j.Element;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class ColComponent extends AbstractPdfComponent {
    private final Element col;
    private final PdfPTable table;
    private final GeneratorContext context;
    private final Element row;
    private VariableContext rootVariableContext;

    public ColComponent(Element col, PdfPTable table, GeneratorContext context, Element row) {
        this.row = row;
        this.col = col;
        this.table = table;
        this.context = context;
    }

    public ColComponent(Element col, PdfPTable table, GeneratorContext context, Element row, VariableContext rootVariableContext) {
        this.row = row;
        this.col = col;
        this.table = table;
        this.context = context;
        this.rootVariableContext = rootVariableContext;
    }

    public PdfPCell getCell() throws DocumentException, IOException {
        PdfPCell cell = PdfUtils.createCell();
        resolveVerticalAlign(cell);
        resolveColStyle(cell);
        List<com.itextpdf.text.Element> elements = createElements();

        if (isAllPhase(elements)) {
            Paragraph paragraph = createParagraph();
            paragraph.addAll(elements);
            cell.addElement(paragraph);
        } else {
            for (com.itextpdf.text.Element element : elements) {
                if (PdfUtils.isPhrase(element)) {
                    Paragraph paragraph = createParagraph(element);
                    cell.addElement(paragraph);
                } else {
                    cell.addElement(element);
                }
            }
        }

        cell.setMinimumHeight(30);
        return cell;
    }

    private Paragraph createParagraph() {
        Paragraph paragraph = PdfUtils.createParagraph();
        resolveColStyle(paragraph);
        return paragraph;
    }

    private boolean isAllPhase(List<com.itextpdf.text.Element> elements) {
        if (CommonUtils.isEmpty(elements)) return true;
        return elements.stream().filter(PdfUtils::isPhrase).collect(Collectors.toList()).size() == elements.size();
    }

    private void resolveVerticalAlign(PdfPCell cell) {
        cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_TOP);
        try {
            String verticalAlign = this.col.attributeValue("verticalAlign");
            if (verticalAlign.equals("center")) {
                cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);
            }
            if (verticalAlign.equals("top")) {
                cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_TOP);
            }
            if (verticalAlign.equals("bottom")) {
                cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_BOTTOM);
            }
        } catch (Exception e) {
        }
    }

    private List<com.itextpdf.text.Element> createElements() throws DocumentException, IOException {
        if (!CommonUtils.isEmpty(rootVariableContext)) {
            return createPdfElementsByVariableContext(rootVariableContext, col, context);
        }
        return parseSimpleNode(col, context);
    }

    private Paragraph createParagraph(com.itextpdf.text.Element element) {
        Phrase phrase = PdfUtils.toPhrase(element);
        phrase.setFont(context.getGeneratorParameters().getFont());
        Paragraph paragraph = createParagraph();

        paragraph.add(phrase);
        return paragraph;
    }

    private void resolveColStyle(Paragraph paragraph) {
        try {
            Attribute align = col.attribute("align");
            if ("left".equals(align.getText())) {
                paragraph.setAlignment(com.itextpdf.text.Element.ALIGN_LEFT);
            } else if ("right".equals(align.getText())) {
                paragraph.setAlignment(com.itextpdf.text.Element.ALIGN_RIGHT);
            } else {
                paragraph.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
            }
        } catch (Exception e) {
        }
    }

    private void resolveColStyle(PdfPCell cell) {
        try {
            Attribute height = row.attribute("height");
            cell.setFixedHeight(Integer.parseInt(height.getText()));
        } catch (Exception e) {

        }
    }

    @Override
    public void parseComponent(Element element, GeneratorContext context) throws Exception {

    }

    @Override
    public String getName() {
        return "col";
    }
}
